package top.pengdev.esoffice.search.controller;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.pengdev.esoffice.common.dto.ResultData;
import top.pengdev.esoffice.common.model.Pager;
import top.pengdev.esoffice.search.consts.SearchScopeConst;
import top.pengdev.esoffice.search.service.SearchService;
import top.pengdev.esoffice.util.ResultDataUtils;

import javax.annotation.Resource;

/**
 * @author pengdev
 * @description 搜索的控制器
 * @date 2020-11-25 09:52
 **/
@RestController
@RequestMapping("/api/v1")
public class SearchController {

    @Resource
    private SearchService searchService;

    @GetMapping("/search")
    public ResultData search(String key, String scope, Pager pager){
        if (! StringUtils.hasLength(key)){
            return ResultDataUtils.success();
        }

        if(scope.isEmpty() || SearchScopeConst.SEARCH_TITLE.equalsIgnoreCase(scope)){
            return ResultDataUtils.success(searchService.searchTitle(key, pager));
        }

        if (SearchScopeConst.SEARCH_CONTENT.equalsIgnoreCase(scope)){
            return ResultDataUtils.success(searchService.searchContent(key, pager));
        }

        return ResultDataUtils.success();
    }
}
